<template>
	<view class="container">
		<!-- 用户信息头部 -->
		<view class="user-header">
			<view class="user-info">
				<image class="user-avatar" :src="userInfo.avatar" mode="aspectFill"></image>
				<view class="user-details">
					<text class="username">{{userInfo.name}}</text>
					<text class="user-level">{{userInfo.level}}</text>
				</view>
			</view>
			<view class="user-stats">
				<view class="stat-item">
					<text class="stat-number">{{userInfo.points}}</text>
					<text class="stat-label">积分</text>
				</view>
				<view class="stat-item">
					<text class="stat-number">{{userInfo.coupons}}</text>
					<text class="stat-label">优惠券</text>
				</view>
				<view class="stat-item">
					<text class="stat-number">{{userInfo.balance}}</text>
					<text class="stat-label">余额</text>
				</view>
			</view>
		</view>

		<!-- 我的订单 -->
		<view class="order-section">
			<view class="section-header">
				<text class="section-title">我的订单</text>
				<text class="view-all" @click="viewAllOrders">查看全部 ></text>
			</view>
			<view class="order-types">
				<view class="order-type-item" v-for="(type, index) in orderTypes" :key="index" @click="goToOrders(type.status)">
					<view class="type-icon">{{type.icon}}</view>
					<text class="type-name">{{type.name}}</text>
					<view class="order-count" v-if="type.count > 0">{{type.count}}</view>
				</view>
			</view>
		</view>

		<!-- 我的服务 -->
		<view class="service-section">
			<view class="section-header">
				<text class="section-title">我的服务</text>
			</view>
			<view class="service-grid">
				<view class="service-item" v-for="(service, index) in services" :key="index" @click="goToService(service.url)">
					<view class="service-icon">{{service.icon}}</view>
					<text class="service-name">{{service.name}}</text>
				</view>
			</view>
		</view>

		<!-- 设置列表 -->
		<view class="settings-section">
			<view class="setting-item" v-for="(setting, index) in settings" :key="index" @click="goToSetting(setting.url)">
				<view class="setting-left">
					<view class="setting-icon">{{setting.icon}}</view>
					<text class="setting-name">{{setting.name}}</text>
				</view>
				<view class="setting-right">
					<text class="setting-value" v-if="setting.value">{{setting.value}}</text>
					<view class="arrow">></view>
				</view>
			</view>
		</view>

		<!-- 退出登录 -->
		<view class="logout-section">
			<button class="logout-btn" @click="handleLogout">退出登录</button>
		</view>
	</view>
</template>

<script>
	import Navigation from '@/utils/navigation.js';
	import Auth from '@/utils/auth.js';
	
	export default {
		data() {
			return {
				activeNav: 'profile',
				userInfo: {
					avatar: '/static/avatar.png',
					name: '用户昵称',
					level: 'VIP会员',
					points: 1280,
					coupons: 5,
					balance: '99.99'
				},
				orderTypes: [
					{ icon: '💰', name: '待付款', status: 'pending', count: 2 },
					{ icon: '📦', name: '待发货', status: 'shipping', count: 1 },
					{ icon: '🚚', name: '待收货', status: 'receiving', count: 3 },
					{ icon: '⭐', name: '待评价', status: 'review', count: 0 }
				],
				services: [
					{ icon: '❤️', name: '我的收藏', url: '/pages/profile/favorites' },
					{ icon: '🎫', name: '优惠券', url: '/pages/profile/coupons' },
					{ icon: '📍', name: '收货地址', url: '/pages/profile/address' },
					{ icon: '💬', name: '客服中心', url: '/pages/profile/service' },
					{ icon: '📱', name: '邀请好友', url: '/pages/profile/invite' },
					{ icon: '🎁', name: '积分商城', url: '/pages/profile/points' },
					{ icon: '📋', name: '意见反馈', url: '/pages/profile/feedback' },
					{ icon: '⚙️', name: '设置', url: '/pages/profile/settings' }
				],
				settings: [
					{ icon: '🔔', name: '消息通知', value: '已开启', url: '/pages/profile/notifications' },
					{ icon: '🔒', name: '隐私设置', url: '/pages/profile/privacy' },
					{ icon: '🌐', name: '语言设置', value: '简体中文', url: '/pages/profile/language' },
					{ icon: '📱', name: '关于我们', url: '/pages/profile/about' }
				]
			}
		},
		onLoad() {
			this.checkLoginStatus();
		},
		onShow() {
			this.checkLoginStatus();
			this.loadUserInfo();
		},
		methods: {
			checkLoginStatus() {
				if (!Auth.isLoggedIn()) {
					Navigation.goToLogin();
				}
			},
			loadUserInfo() {
				const userInfo = Auth.getUserInfo();
				if (userInfo) {
					this.userInfo.name = userInfo.username || '用户昵称';
					this.userInfo.avatar = userInfo.avatar || '/static/avatar.png';
				}
			},
			viewAllOrders() {
				Navigation.goToList({category: '我的订单'});
			},
			goToOrders(status) {
				Navigation.goToList({category: '我的订单', status: status});
			},
			goToService(url) {
				if (url.includes('favorites')) {
					Navigation.goToList({category: '我的收藏'});
				} else if (url.includes('coupons')) {
					Navigation.goToCoupon({mode: 'manage'});
				} else if (url.includes('address')) {
					Navigation.goToAddress({mode: 'manage'});
				} else {
					// 其他功能显示开发中提示
					uni.showToast({
						title: '功能开发中',
						icon: 'none'
					});
				}
			},
			goToSetting(url) {
				// 设置功能显示开发中提示
				uni.showToast({
					title: '功能开发中',
					icon: 'none'
				});
			},
			handleLogout() {
				uni.showModal({
					title: '确认退出',
					content: '确定要退出登录吗？',
					confirmText: '确定退出',
					confirmColor: '#ff4757',
					success: (res) => {
						if (res.confirm) {
							Auth.logout();
						}
					}
				});
			}
		}
	}
</script>

<style scoped>
.container {
	background-color: #f5f5f5;
	min-height: 100vh;
	padding-bottom: 120rpx;
}

.user-header {
	background: linear-gradient(135deg, #ff6b35, #ff8c42);
	padding: 40rpx 30rpx;
	color: white;
}

.user-info {
	display: flex;
	align-items: center;
	gap: 20rpx;
	margin-bottom: 30rpx;
}

.user-avatar {
	width: 100rpx;
	height: 100rpx;
	border-radius: 50rpx;
	border: 4rpx solid rgba(255, 255, 255, 0.3);
}

.user-details {
	display: flex;
	flex-direction: column;
	gap: 10rpx;
}

.username {
	font-size: 36rpx;
	font-weight: bold;
}

.user-level {
	font-size: 24rpx;
	opacity: 0.9;
}

.user-stats {
	display: flex;
	justify-content: space-around;
}

.stat-item {
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 8rpx;
}

.stat-number {
	font-size: 32rpx;
	font-weight: bold;
}

.stat-label {
	font-size: 24rpx;
	opacity: 0.9;
}

.order-section {
	background-color: #fff;
	margin: 20rpx;
	border-radius: 20rpx;
	padding: 30rpx;
}

.section-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 30rpx;
}

.section-title {
	font-size: 32rpx;
	font-weight: bold;
	color: #333;
}

.view-all {
	font-size: 26rpx;
	color: #666;
}

.order-types {
	display: flex;
	justify-content: space-around;
}

.order-type-item {
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 15rpx;
	position: relative;
}

.type-icon {
	font-size: 50rpx;
}

.type-name {
	font-size: 24rpx;
	color: #666;
}

.order-count {
	position: absolute;
	top: -10rpx;
	right: -10rpx;
	background-color: #ff4757;
	color: white;
	font-size: 20rpx;
	padding: 4rpx 8rpx;
	border-radius: 10rpx;
	min-width: 20rpx;
	text-align: center;
}

.service-section {
	background-color: #fff;
	margin: 0 20rpx 20rpx;
	border-radius: 20rpx;
	padding: 30rpx;
}

.service-grid {
	display: grid;
	grid-template-columns: repeat(4, 1fr);
	gap: 30rpx;
}

.service-item {
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 15rpx;
}

.service-icon {
	font-size: 50rpx;
}

.service-name {
	font-size: 24rpx;
	color: #666;
	text-align: center;
}

.settings-section {
	background-color: #fff;
	margin: 0 20rpx;
	border-radius: 20rpx;
	overflow: hidden;
}

.setting-item {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 30rpx;
	border-bottom: 1rpx solid #f0f0f0;
}

.setting-item:last-child {
	border-bottom: none;
}

.setting-left {
	display: flex;
	align-items: center;
	gap: 20rpx;
}

.setting-icon {
	font-size: 40rpx;
}

.setting-name {
	font-size: 28rpx;
	color: #333;
}

.setting-right {
	display: flex;
	align-items: center;
	gap: 15rpx;
}

.setting-value {
	font-size: 24rpx;
	color: #999;
}

.arrow {
	font-size: 24rpx;
	color: #999;
}

.logout-section {
	padding: 40rpx 30rpx;
}

.logout-btn {
	width: 100%;
	height: 90rpx;
	background-color: #ff4757;
	color: white;
	border: none;
	border-radius: 15rpx;
	font-size: 32rpx;
	font-weight: bold;
	transition: all 0.3s ease;
}

.logout-btn:active {
	background-color: #ff3742;
	transform: scale(0.98);
}
</style>
